package com.nidu.demo.file.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.file.convertor.FileConfigConvertor;
import com.nidu.demo.file.gateway.FileConfigGateway;
import com.nidu.demo.file.mapper.FileConfigMapper;
import com.nidu.demo.file.mapper.dataobject.FileConfigDO;
import com.nidu.demo.file.model.FileConfig;
import com.nidu.demo.file.model.FileConfigCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 文件配置网关实现
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class FileConfigGatewayImpl implements FileConfigGateway {

    private final FileConfigMapper mapper;
    private final FileConfigConvertor convertor;

    @Override
    public FileConfig create(FileConfig fileConfig) {
        FileConfigDO fileConfigDO = convertor.toDataObject(fileConfig);
        mapper.insert(fileConfigDO);
        return convertor.toDomainObject(fileConfigDO);
    }

    @Override
    public FileConfig update(FileConfig fileConfig) {
        FileConfigDO fileConfigDO = convertor.toDataObject(fileConfig);
        mapper.updateById(fileConfigDO);
        return convertor.toDomainObject(fileConfigDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public FileConfig getById(Long id) {
        FileConfigDO fileConfigDO = mapper.selectById(id);
        if (fileConfigDO == null) {
            return null;
        }
        return convertor.toDomainObject(fileConfigDO);
    }

    @Override
    public List<FileConfig> getByIds(Set<Long> ids) {
        LambdaQueryWrapper<FileConfigDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FileConfigDO::getId, ids);
        List<FileConfigDO> fileConfigDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(fileConfigDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(fileConfigDOS);
    }

    @Override
    public List<FileConfig> listByCondition(FileConfigCriteria criteria) {
        LambdaQueryWrapper<FileConfigDO> wrapper = buildQueryWrapper(criteria);
        List<FileConfigDO> fileConfigDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(fileConfigDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(fileConfigDOS);
    }

    @Override
    public PageResponse<FileConfig> pageByCondition(FileConfigCriteria criteria) {
        LambdaQueryWrapper<FileConfigDO> wrapper = buildQueryWrapper(criteria);
        IPage<FileConfigDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());
        IPage<FileConfigDO> result = mapper.selectPage(page, wrapper);
        
        PageResponse<FileConfig> pageResponse = new PageResponse<>();
        pageResponse.setPageIndex((int) result.getCurrent());
        pageResponse.setPageSize((int) result.getSize());
        pageResponse.setTotalCount((int) result.getTotal());
        pageResponse.setData(convertor.toDomainObjects(result.getRecords()));
        return pageResponse;
    }

    private LambdaQueryWrapper<FileConfigDO> buildQueryWrapper(FileConfigCriteria criteria) {
        LambdaQueryWrapper<FileConfigDO> wrapper = new LambdaQueryWrapper<>();
        
        // 时间范围条件
        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(FileConfigDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(FileConfigDO::getCreateTime, createTime[0], createTime[1]);
            }
        }

        // 模糊查询条件
        wrapper.like(!StrUtil.isEmpty(criteria.getName()), FileConfigDO::getName, criteria.getName());
        
        // 精确查询条件
        wrapper.eq(criteria.getStorage() != null, FileConfigDO::getStorage, criteria.getStorage());
        wrapper.eq(criteria.getMaster() != null, FileConfigDO::getMaster, criteria.getMaster());
        
        return wrapper;
    }

}
